import 'package:flutter/material.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/my_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';

class MyPageTemp extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();
  final MyPageController controller = Get.put(MyPageController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      body: GetBuilder<MyPageController>(
        init: MyPageController(),
        builder: (controller) {
          return Stack(
            children: [
              Obx(() => AnimatedContainer(
                duration: Duration(milliseconds: 1),
                height: controller.bgHeight.value + ScreenUtil().statusBarHeight,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.bottomCenter,
                    end: Alignment.topCenter,
                    colors: [
                      bgPinkColor,
                      bgMainColor
                    ],
                  )
                ),
              )),
              Positioned(
                child: Column(
                  children: [
                    Obx(() => Container(
                      height: 40.h + ScreenUtil().statusBarHeight,
                      padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                          colors: [
                            Color.fromRGBO(254, 7, 15, controller.titleBarOpacity.value),
                            Color.fromRGBO(255, 38, 99, controller.titleBarOpacity.value)
                          ],
                        )
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Container(
                            margin: EdgeInsets.only(right: 16.w),
                            child: InkWellUtil.shakeProofInkWell(
                              Image.asset(
                                'assets/icons/icon_setting.png',
                                width: 20.w,
                                height: 20.w,
                              ), () {
                                Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}');
                              },
                              hoverColor: Colors.transparent,
                              splashColor: Colors.transparent,
                              highlightColor: Colors.transparent
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(right: 16.w),
                            child: InkWellUtil.shakeProofInkWell(
                              Image.asset(
                                'assets/icons/icon_msg.png',
                                width: 20.w,
                                height: 20.w,
                              ), () {
                                if (commonPageController.isRevirew.value) {
                                  commonPageController.tabIndex.value = 1;
                                } else {
                                  commonPageController.tabIndex.value = 2;
                                }
                              },
                              hoverColor: Colors.transparent,
                              splashColor: Colors.transparent,
                              highlightColor: Colors.transparent
                            ),
                          ),
                        ],
                      ),
                    )),
                    Obx(() => Expanded(child: RefreshUtil.easyRefresh(
                      scrollController: controller.scrollController,
                      onRefresh: () async {
                        commonPageController.checkLogin(loading: false);
                      },
                      slivers: [
                        SliverToBoxAdapter(child: _userHead()),
                        SliverToBoxAdapter(child: _orderBody(title: '商城订单', children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              _funItem(path: 'assets/icons/icon_payment.png', title: '待付款', num: commonPageController.userData.value.statistics!.shopping![0], width: 65.w, onTap: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}',
                                  arguments: {
                                    'tabIndex': 1
                                  }
                                );
                              }),
                              _funItem(path: 'assets/icons/icon_store_received.png', title: '待收货', num: commonPageController.userData.value.statistics!.shopping![1], width: 65.w, onTap: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}',
                                  arguments: {
                                    'tabIndex': 2
                                  }
                                );
                              }),
                              _funItem(path: 'assets/icons/icon_exchange.png', title: '退换/售后', num: commonPageController.userData.value.statistics!.shopping![2], width: 65.w, onTap: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}',
                                  arguments: {
                                    'tabIndex': 0
                                  }
                                );
                              }),
                              // _funItem(path: 'assets/icons/icon_gift_l.png', title: '赠品', num: commonPageController.userData.value.statistics!.shopping![2], width: 65.w, onTap: () {
                              //   Get.toNamed(
                              //     '${AppRouters.COMMON_PAGE}${AppRouters.GIFT_ORDER_PAGE}'
                              //   );
                              // })
                            ],
                          )
                        ], onTap: () {
                          Get.toNamed(
                            '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}',
                            arguments: {
                              'tabIndex': 0
                            }
                          );
                        })),
                        SliverToBoxAdapter(child: Container(
                          margin: EdgeInsets.only(top: 12.h),
                          padding: EdgeInsets.symmetric(horizontal: 16.w),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(5.w),
                            child: Image.asset(
                              'assets/images/preferential_bg.jpg',
                              width: 343.w,
                              height: 130.h,
                              fit: BoxFit.cover,
                            ),
                          ),
                        )),
                        SliverToBoxAdapter(child: SizedBox(height: 38.h))
                      ]
                    )))
                  ],
                )
              )
            ],
          );
        },
      ),
    );
  }

  /// 用户信息
  Widget _userHead() {
    if (!commonPageController.isAuthenticated.value) {
      return InkWellUtil.shakeProofInkWell(Container(
        height: 54.w,
        padding: EdgeInsets.only(left: 30.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Image.asset(
                  'assets/icons/icon_default_head.png',
                  width: 54.w,
                  height: 54.w,
                ),
                SizedBox(width: 12.w),
                Text(
                  '请点击登录',
                  style: TextStyle(
                    color: textWhiteColor,
                    fontSize: 18.sp,
                    fontWeight: FontWeight.bold
                  ),
                )
              ],
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.h),
              decoration: BoxDecoration(
                color: bgWhiteColor_50,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(20.w),
                  bottomLeft: Radius.circular(20.w)
                )
              ),
              child: Text(
                '游客身份',
                style: TextStyle(
                  color: textWhiteColor,
                  fontSize: 12.sp
                ),
              ),
            )
          ],
        ),
      ), () {
        Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.LOGIN_PAGE}');
      });
    }
    return InkWellUtil.shakeProofInkWell(Container(
      height: 54.w,
      padding: EdgeInsets.only(left: 30.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              NetworkImageUtil(
                url: commonPageController.userData.value.avatar ?? '',
                nickname: commonPageController.userData.value.nickname ?? '', 
                width: 54.w, 
                height: 54.w
              ),
              SizedBox(width: 12.w),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    commonPageController.userData.value.nickname ?? '',
                    style: TextStyle(
                      color: textWhiteColor,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold
                    ),
                  ),
                  SizedBox(height: 6.w),
                  Text(
                    '查看或编辑个人主页',
                    style: TextStyle(
                      color: textWhiteColor,
                      fontSize: 14.sp
                    ),
                  ),
                ],
              )
            ],
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.h),
            decoration: BoxDecoration(
              color: bgWhiteColor_50,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20.w),
                bottomLeft: Radius.circular(20.w)
              )
            ),
            child: Text(
              commonPageController.userData.value.levelStr ?? '',
              style: TextStyle(
                color: textWhiteColor,
                fontSize: 12.sp
              ),
            ),
          )
        ],
      ),
    ), () {
      Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}');
    });
  }

  /// 订单Body
  Widget _orderBody({required String title, required List<Widget> children, bool showAllBtn = true,bool topPadding = true, Function? onTap}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      margin: EdgeInsets.only(top: 16.h),
      child: Container(
        decoration: BoxDecoration(
          color: bgWhiteColor,
          borderRadius: BorderRadius.circular(5.w)
        ),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              child: Container(
                height: 54.h,
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: lineColor,
                      width: 1.w
                    )
                  )
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    ),
                    showAllBtn ? InkWellUtil.shakeProofInkWell(Row(
                      children: [
                        Text(
                          '全部订单',
                          style: TextStyle(
                            color: textSubtitleColor,
                            fontSize: 14.sp
                          ),
                        ),
                        SizedBox(width: 8.w),
                        Image.asset(
                          'assets/icons/icon_arrow_right_w.png',
                          width: 8.w,
                          height: 15.h,
                        )
                      ],
                    ), () {
                      onTap!();
                    }) : Container()
                  ],
                ),
              ),
            ),
            Container(
              width: 343.w,
              padding: EdgeInsets.only(top: topPadding ? 12.h : 0, bottom: 20.h),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: children,
              ),
            ),
            SizedBox(height: 4.h)
          ],
        ),
      ),
    );
  }

  /// 功能项
  Widget _funItem({required String title, required path, required double width, required Function onTap, int num = 0}) {
    return InkWellUtil.shakeProofInkWell(Stack(
      children: [
        Container(
          width: width,
          padding: EdgeInsets.only(top: 6.h),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Image.asset(
                path,
                width: 24.w,
                height: 24.w,
              ),
              SizedBox(height: 8.h),
              Text(
                title,
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 14.sp
                ),
              )
            ],
          ),
        ),
        Positioned(
          top: 0,
          right: num <= 99 ? num < 10 ? 12.w : 6.w : 0,
          child: _angleMark(num)
        )
      ],
    ), () => onTap());
  }

  /// 角标
  Widget _angleMark(int number) {
    if (number <= 0) {
      return Container();
    }
    if (number < 10) {
      return Container(
        width: 16.w,
        height: 16.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16.w),
          color: bgMainColor,
          border: Border.all(
            width: 1.w,
            color: bgWhiteColor
          )
        ),
        child: Text(
          '$number',
          style: TextStyle(
            color: textWhiteColor,
            fontSize: 12.sp
          ),
        ),
      );
    }
    if (number <= 99) {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 1.h),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16.w),
          color: bgMainColor,
          border: Border.all(
            width: 1.w,
            color: bgWhiteColor
          )
        ),
        child: Text(
          '$number',
          style: TextStyle(
            color: textWhiteColor,
            fontSize: 12.sp
          ),
        ),
      );
    }
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 1.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16.w),
        color: bgMainColor,
        border: Border.all(
          width: 1.w,
          color: bgWhiteColor
        )
      ),
      child: Text(
        '99+',
        style: TextStyle(
          color: textWhiteColor,
          fontSize: 12.sp
        ),
      ),
    );
  }
}